<?php


namespace app\zmt\controller;


use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\AddShortUrlResponseBody\data;
use app\zmt\model\goodsCateValueModel;
use app\zmt\model\goodsModel;
use cmf\controller\AdminBaseController;
use think\Db;

class goodsController extends AdminBaseController
{
    //进行自选框数据定义
    protected $statusMap = [
        0 => '缺货',
        1 => '上架',
        2 => '下架',
    ];
    protected $Map = [
        0 => '自营',
        1 => '旗舰店',
        2 => '非自营',
    ];
    //主页显示，进行搜索
    public function index(){
        $name = input('name');
        $where = [];
        if ($name){
            $where[] = ['name','like','%'.$name.'%'];
        }
        $data = goodsModel::with('cate')->where($where)->select();
        return $this->fetch('index',[
            'data' => $data,
            'statusMap' =>$this->statusMap,
            'Map' =>$this->Map,
        ]);
    }
    //添加页面，调用数据选择框
    public function add(){
        return $this->fetch('add',[
            'data' => $this->statusMap,
            'data1' => $this->Map,
        ]);
    }
    //添加数据
    public function addPost(){
        $data = input('post.');

        $data['chart'] = implode(',',$data['chart']);
        $data['create_time'] = date('Y-m-d H:i:s');
        $model = goodsModel::create($data);
        if ($model){
            return json(['code'=>1,'msg'=>'添加成功','data'=>$data]);
        }else{
            return json(['code'=>0,'msg'=>'添加失败','data'=>$data]);
        }
    }
    public function edit($id){
        $data = goodsModel::find($id);
        return $this->fetch('edit',[
            'data' => $data
        ]);
    }
    //列表数字排列
    public function order(){
        parent::listOrders(new goodsModel());
        $this->success('排序成功');
    }
    //货品状态类型切换
    public function changeStatus()
    {
        $id = input('id', 0, 'intval');
        $goods = goodsModel::find($id);
        if ($goods) {
            // 切换状态：缺货->上架->下架->缺货
            if ($goods->status == 0) {
                $goods->status = 1;
            } elseif ($goods->status == 1) {
                $goods->status = 2;
            } else {
                $goods->status = 0;
            }
            if ($goods->save()) {
                $this->success('状态切换成功', 'goods/index');
            } else {
                $this->error('状态切换失败', 'goods/index');
            }
        } else {
            $this->error('商品不存在', 'goods/index');
        }
    }
    //营店类型切换
    public function changeStatustwo()
    {
        $id = input('id', 0, 'intval');
        $goods = goodsModel::find($id);
        if ($goods) {
            // 切换状态：自营->旗舰店->非自营->自营
            if ($goods->switch == 0) {
                $goods->switch = 1;
            } elseif ($goods->switch == 1) {
                $goods->switch = 2;
            } else {
                $goods->switch = 0;
            }
            if ($goods->save()) {
                $this->success('状态切换成功', 'goods/index');
            } else {
                $this->error('状态切换失败', 'goods/index');
            }
        } else {
            $this->error('商品不存在', 'goods/index');
        }
    }
    //数据显示，关闭切换
    public function totolgSwitchtwo(){
        $id = input('id/d');
        $user = goodsModel::find($id);
        if (!$user) {
            $this->error('主题不存在');
        }
        // 切换状态
        $user->switchtwo = $user->switchtwo ? 0 : 1;
        if ($user->save()) {
            $this->success('切换成功');
        } else {
            $this->error('切换失败');
        }
    }
    //编辑接受数据上传
    public function editPost(){
        $data = input('post.');
        $data['chart'] = implode(',',$data['chart']);
        $model = goodsModel::find($data['id']);
        if(empty($model)){
           return json(['code'=>0,'msg'=>'无该商品','data'=>null]);
        }
        $model->name = $data['name'];
        $model->image = $data['image'];
        $model->chart = $data['chart'];
        $model->title = $data['title'];
        $model->status = $data['status'];
        $model->PrimaryPrice = $data['PrimaryPrice'];
        $model->DiscountPrice = $data['DiscountPrice'];
        $model->switch = $data['switch'];
        $model->number = $data['number'];
        if($model->save()){
            return json(['code'=>1,'msg'=>'修改成功','data'=>$model]);
        }else{
            return json(['code'=>0,'msg'=>'修改成功','data'=>null]);
        }
    }
    //数据删除
    public function delete(){
        $id = input('id');
        if (goodsModel::destroy($id)){
            $this->success('删除成功');
        }else {
            $this->success('删除成功');
        }
    }

    public function setGoodsAttr(){
        $goodsId = input('goods_id');
        return $this->fetch('goods_attr',[
            'goods_id' =>$goodsId
        ]);
    }
    public function setGoodsAttrPost(){
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');

        if (count($attrs)<1){
            $this->error('请至少配置一个属性');
        }
        Db::name('goods_attr')->where('goods_id',$goodsId)->delete();
        $insert_data = [];
        foreach ($attrs as $attr){
            $insert_data[] = [
                'goods_id' =>$goodsId,
                'name' => $attr
            ];
        }
        \think\facade\Db::name('goods_attr')->insertAll($insert_data);
        $this->success('保存成功');
    }
    //属性值页码属性显示
    public function setGoodsAttrValue(){
        $goodsId = input('goods_id');
        return $this->fetch('goods_attr_value',[
            'goods_id' =>$goodsId
        ]);
    }
    //页码属性值添加
    public function setGoodsAttrValuePost(){

        $data = input('post.form');
        if (count($data)<1){
            return json(['code'=>0,'msg'=>'请输入一个属性值','data'=>null]);
        }
        $insert_data =[];
        foreach ($data as $attrs){
            $insert_data[] =[
                'attr_id'=>$attrs['attr'],'attr_value'=>$attrs['attr_value']
            ];
        }
        if (goodsCateValueModel::insertALL($insert_data)) {
            return json(['code'=>1,'msg'=>'添加成功','data'=>null]);
        }

    }
    //属性值添加显示
    public function getGoodsAttr(){
        $goodsId = input('goods_id');
        $attrs =\think\facade\Db::name('goods_attr')
            ->where('goods_id',$goodsId)
            ->select();
        return json(['code'=>1,'msg'=>'商品属性','data'=>$attrs]);
    }
    //属性和属性值页码显示
    public function setGoodsAttrdata(){
        $goodsId = input('goods_id');
        return $this->fetch('goods_attrdata',[
            'goods_id' =>$goodsId
        ]);
    }
    public function setGoodsAttrdataPost(){
        $goods_id  = input('goods_id');
        $data = input('post.form');
        if (goodsModel::where('id',$goods_id)->update([ 'attr'=>json_encode($data,JSON_UNESCAPED_UNICODE)])) {
            return json(['code'=>1,'msg'=>'商品添加成功属性','data'=>null]);
        }

    }
}